package flst.ipiweb.struts.action.administrateur;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.proxy.ServiceAdministrateurProxy;
import flst.ipiweb.transverse.Administrateur;
import flst.ipiweb.transverse.IPIException;

public class UpdateAdminAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		//On récupère la session
		HttpSession session = request.getSession();
		if(session.getAttribute("login") == null)
		{
			return mapping.findForward("login");
		}
		
		int idUser = -1;
		
		try{
			idUser = Integer.parseInt(request.getParameter("id"));
		}catch(Exception e){
			return mapping.findForward("list_admin");
		}
		
		//Instanciation du service administrateur
		ServiceAdministrateurProxy sAdmin = new ServiceAdministrateurProxy((Boolean) session.getAttribute("isSuper"));
		
		try {
			
			Administrateur admin = sAdmin.getAdministrateurById(idUser);
			request.setAttribute("admin", admin);
			
		} catch (IPIException e) {
			
			session.setAttribute("error", e.getMessage());
			return mapping.findForward("list_admin");
		}
		
		//Titre de la page
		request.setAttribute("pageTitle", "admin.update.title");
		request.setAttribute("mainTitle", "admin.update.mainTitle");
		//Action du formulaire
		request.setAttribute("action", "/update-admin-post");
		
		//Activation de l'onglet Administrateur
		HashMap<String, String> onglet = new HashMap<String, String>();
		onglet.put("administrateur", "active");
		request.setAttribute("onglet", onglet);
		
		return mapping.findForward("update_admin");
	}
}
